package com.example.springboot_demo.service.impl;

import com.example.springboot_demo.dto.Goods;
import com.example.springboot_demo.dto.request.GoodsRequest;
import com.example.springboot_demo.mapper.GoodsMapper;
import com.example.springboot_demo.service.GoodsService;
import com.example.springboot_demo.utils.AjaxResult;
import com.example.springboot_demo.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/6/23:16:58
 */

@SuppressWarnings({"unchecked", ""})
@Service
public class GoodsServiceImpl implements GoodsService {
    private static final Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);


    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public Goods findById(Integer id) {
        log.info("根据id查询商品信,id:{}",id);
        Goods goods = goodsMapper.findById(id);
        return goods;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public AjaxResult addGoods(GoodsRequest goodsRequest) {

        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest,goods);

        goodsMapper.addGoods(goods);
        update(goodsRequest);
        return AjaxResult.success();
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public AjaxResult update(GoodsRequest goodsRequest) {

        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest,goods);

        goodsMapper.addGoods(goods);

        return AjaxResult.success();
    }


    @Autowired
    private GoodsService goodsService;

    @Override
    public AjaxResult testTransactional1(GoodsRequest goodsRequest) throws IOException {
        goodsService.testTransactional2(goodsRequest);
        //throw new IOException();
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult testTransactional2(GoodsRequest goodsRequest ) throws IOException {
        //goodsMapper.insertSelective(goods);
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest,goods);
        goodsMapper.addGoods(goods);
        log.info("新生成的id是 {}",goods.getId());
        int i = 1/0;
        try {
            //int i = 1/0;
        }catch (Exception e){
            log.info("{}",e);
        }
        return null;
    }

    @Override
    public AjaxResult findByPage(GoodsRequest goodsRequest) {

        PageHelper.startPage(goodsRequest.getPageNum(),goodsRequest.getPageSize());
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest,goods);
        List<Goods> goodsList = goodsMapper.findByDyWhere(goods);
        PageInfo<Goods> of = PageInfo.of(goodsList);
        PageUtils pageUtils = new PageUtils(of);

        return AjaxResult.success(pageUtils);
    }


}
